//
//  WLContentsListTableView.swift
//  VomLib
//
//  Created by Delin Meng 孟德林 on 2018/9/4.
//  Copyright © 2018年 Yongpeng Zhu 朱永鹏. All rights reserved.
//

import UIKit

private let citySelectViewCellIdentifier = "citySelectViewCell"

class WLContentsListTableView: UITableView {

    var provincesArray = [String]() {
        didSet {
            self.isProvincesTableView = true
            self.reloadData()
        }
    }
    var citiesArray = [String]() {
        didSet {
            self.isProvincesTableView = false
            self.reloadData()
        }
    }
    var currentProvince: String? {
        didSet {
            self.reloadData()
        }
    }
    var provinceSelected: ((String?) -> Void)?
    var citySelected: ((String?) -> Void)?
    
    fileprivate var isProvincesTableView = true
    
    convenience init() {
        self.init(frame: CGRect.zero, style: .plain)
    }
    override init(frame: CGRect, style: UITableView.Style) {
        super.init(frame: frame, style: style)
        
        self.backgroundColor = .white
        self.showsVerticalScrollIndicator = false
        self.separatorStyle = .none
        self.tableFooterView = UIView()
        self.register(WLContentSelectViewCell.self, forCellReuseIdentifier: citySelectViewCellIdentifier)
        self.delegate = self
        self.dataSource = self
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension WLContentsListTableView:UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if isProvincesTableView {
            return provincesArray.count
        }else {
            return citiesArray.count
        }
        
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = self.dequeueReusableCell(withIdentifier: citySelectViewCellIdentifier, for: indexPath) as? WLContentSelectViewCell
        if isProvincesTableView {
            cell?.provinceOrCityString = provincesArray[indexPath.row]
            //显示选中图片
            if currentProvince == provincesArray[indexPath.row] {
                cell?.cellDidSelect()
            }else {
                cell?.cellDidDeselect()
            }
        }else {
            cell?.provinceOrCityString = citiesArray[indexPath.row]
        }
        
        cell?.selectionStyle = .none
        return cell!
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 38
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let cell = tableView.cellForRow(at: indexPath) as? WLContentSelectViewCell
        if isProvincesTableView {
            guard self.provinceSelected?(provincesArray[indexPath.row]) != nil else {
                return
            }
            currentProvince = provincesArray[indexPath.row]
            cell?.cellDidSelect()
        }else {
            guard self.citySelected?(citiesArray[indexPath.row]) != nil else {
                return
            }
        }
    }
    
    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        let cell = tableView.dequeueReusableCell(withIdentifier: citySelectViewCellIdentifier, for: indexPath) as? WLContentSelectViewCell
        cell?.cellDidDeselect()
    }
}


class WLContentSelectViewCell: UITableViewCell {
    
    var provinceOrCityString = String() {
        didSet{
            self.label.text = provinceOrCityString
        }
    }
    private lazy var provinceDidSelectImageView: UIImageView = {
        let provinceDidSelectImageView = UIImageView()
        provinceDidSelectImageView.image = UIImage.init(named: "configure_province_select", in: nil, compatibleWith: nil)
        provinceDidSelectImageView.translatesAutoresizingMaskIntoConstraints = false
        provinceDidSelectImageView.isHidden = true
        return provinceDidSelectImageView
    }()
    
    lazy var label: UILabel = {
        
        let label = UILabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        label.textColor = UIColor.init("#454553")
        label.font = UIFont.systemFont(ofSize: 12)
        return label
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.contentView.addSubview(label)
        let labelLeftConstraint = NSLayoutConstraint.init(item: label, attribute: .left, relatedBy: .equal, toItem: self.contentView, attribute: .left, multiplier: 1, constant: 24.5)
        let labelTopConstraint = NSLayoutConstraint.init(item: label, attribute: .top, relatedBy: .equal, toItem: contentView, attribute: .top, multiplier: 1, constant: 10)
        let labelHeightConstraint = NSLayoutConstraint.init(item: label, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 18)
        self.contentView.addConstraints([labelLeftConstraint, labelTopConstraint, labelHeightConstraint])
        
        self.contentView.addSubview(provinceDidSelectImageView)
        let imageViewLeftConstraint = NSLayoutConstraint.init(item: provinceDidSelectImageView, attribute: .left, relatedBy: .equal, toItem: label, attribute: .right, multiplier: 1, constant: 3)
        let imageViewCenterYConstraint = NSLayoutConstraint.init(item: provinceDidSelectImageView, attribute: .centerY, relatedBy: .equal, toItem: label, attribute: .centerY, multiplier: 1, constant: 0)
        let imageViewWidthConstraint = NSLayoutConstraint.init(item: provinceDidSelectImageView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 10.2)
        let imageViewHeightConstraint = NSLayoutConstraint.init(item: provinceDidSelectImageView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 7)
        self.contentView.addConstraints([imageViewLeftConstraint, imageViewCenterYConstraint, imageViewWidthConstraint, imageViewHeightConstraint])
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: public methods
    public func cellDidSelect() {
        self.provinceDidSelectImageView.isHidden = false
        self.label.textColor = UIColor.lightGray
    }
    public func cellDidDeselect() {
        self.provinceDidSelectImageView.isHidden = true
        self.label.textColor = UIColor.init("#454553")
    }
}
